package com.ljw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljw.dto.GetFreeRoomDto;
import com.ljw.entity.Book;
import com.ljw.entity.Move;
import com.ljw.entity.Room;
import com.ljw.mapper.RoomMapper;
import com.ljw.service.BookService;
import com.ljw.service.MoveService;
import com.ljw.service.RoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljw.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客房信息表 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2020-07-25
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    @Autowired
    private MoveService moveService;

    @Autowired
    private BookService bookService;

    @Autowired
    private RoomMapper roomMapper;

    @Override
    @Transactional
    public List<Room> getFreeRoom(GetFreeRoomDto getFreeRoomDto) {

        Integer roomTypeId = getFreeRoomDto.getRoomTypeId();
        String inDate = getFreeRoomDto.getInDate();
        String outDate = getFreeRoomDto.getOutDate();
        //search in room
        QueryWrapper<Room> roomQueryWrapper = new QueryWrapper<>();
        roomQueryWrapper.select("room_id").eq("room_type_id",roomTypeId);
        List<Room> roomList = list(roomQueryWrapper);
        List<Integer> totalRoomIds = new ArrayList<>();
        roomList.forEach(list -> totalRoomIds.add(list.getRoomId()));
        //search in book
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.select("book_room_id").in("book_room_id", totalRoomIds).le("book_in_date", outDate).or()
                .in("book_room_id", totalRoomIds).ge("book_out_date", inDate);
        List<Book> bookList = bookService.list(bookQueryWrapper);
        List<Integer> roomIdsInBook = new ArrayList<>();
        bookList.forEach(list -> roomIdsInBook.add(list.getBookRoomId()));
        totalRoomIds.removeAll(roomIdsInBook);
        //search in move
        QueryWrapper<Move> moveQueryWrapper = new QueryWrapper<>();
        moveQueryWrapper.select("move_room_id").in("move_room_id", totalRoomIds).le("move_in_date", outDate)
                .or().in("move_room_id", totalRoomIds).ge("move_out_date", inDate);
        List<Move> moveList = moveService.list(moveQueryWrapper);
        List<Integer> roomIdsInMove = new ArrayList<>();
        moveList.forEach(list -> roomIdsInMove.add(list.getMoveRoomId()));
        totalRoomIds.removeAll(roomIdsInMove);
        //search in room
        QueryWrapper<Room> resultQueryWrapper = new QueryWrapper<>();
        resultQueryWrapper.in("room_id",totalRoomIds);
        return list(resultQueryWrapper);
    }

    @Override
    public Page<RoomVo> findAll(Page<RoomVo> page) {
        return roomMapper.findAll(page);
    }
}
